package com.kgc.controller;

import cn.hutool.core.util.NumberUtil;
import com.kgc.dto.Code;
import com.kgc.dto.Result;
import com.kgc.entity.Emp;
import com.kgc.exception.BusinessException;
import com.kgc.service.EmpService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: luo
 * @date: 2024/4/5 18:25
 * @Description:
 * @Version: 1.0
 */
@Controller
@RequestMapping("/emp")
@Slf4j
public class EmpController {

    @Autowired
    private EmpService empService;

    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Result findById(@PathVariable("id") String id){
        log.info("empno:{}",id);
        if (!NumberUtil.isNumber(id)){
            throw new BusinessException("参数不匹配",Code.BUSINESS_ERROR);
        }
        Emp emp = empService.findById(Integer.parseInt(id));
        int code = emp!=null?Code.SELECT_SUCCESS:Code.SELECT_ERROR;
        String msg = emp!=null?"查询成功":"查询失败";
        return new Result(emp,code,msg);
    }

    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    @ResponseBody
    public Result findAll(){
        List<Emp> empList = empService.findAll();
        int code = empList!=null?Code.SELECT_SUCCESS:Code.SELECT_ERROR;
        String msg = empList!=null?"查询成功":"查询失败";
        return new Result(empList,code,msg);
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Result add(@RequestBody Emp emp){
        try {
            empService.addEmp(emp);
            return new Result(true,Code.INSERT_SUCCESS,"添加成功");
        }catch (Exception ex){
            return new Result(false,Code.INSERT_ERROR,"添加失败,"+ex.getMessage());
        }
    }

    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    @ResponseBody
    public Result update(@RequestBody Emp emp){
        try {
            empService.updateEmp(emp);
            return new Result(true,Code.UPDATE_SUCCESS,"修改成功");
        }catch (Exception ex){
            return new Result(false,Code.UPDATE_ERROR,"修改失败,"+ex.getMessage());
        }
    }

    @RequestMapping(value = "/delete/{ids}",method = RequestMethod.DELETE)
    @ResponseBody
    public Result delete(@PathVariable("ids") Integer[] ids){
        try {
            empService.deleteEmp(ids);
            return new Result(true,Code.DELETE_SUCCESS,"删除成功");
        }catch (Exception ex){
            return new Result(false,Code.DELETE_ERROR,"删除失败,"+ex.getMessage());
        }
    }

    @RequestMapping(value =  "/page",method = RequestMethod.GET)
    @ResponseBody
    public Result findPage(Integer pageNum,Integer pageSize){
        System.out.println("pageNum:"+pageNum+",pageSize:"+pageSize);
        List<Emp> empList = empService.findPage(pageNum,pageSize);
        int code = empList!=null?Code.SELECT_SUCCESS:Code.SELECT_ERROR;
        String msg = empList!=null?"查询成功":"查询失败";

        int total = empService.findCount();
        return new Result(empList,code,msg,total);
    }

    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    @ResponseBody
    public String upload(String name, MultipartFile[] files){
        int upload=empService.upload(name,files);
        if(upload==0){
            return "上传失败";
        }
        return "上传成功";
    }
}
